package cn.itcast.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

@Controller
@RequestMapping("/upload")
public class FileUploadController {
    @RequestMapping("/show")
    public String fileUpload(HttpServletRequest req, MultipartFile upload){
        System.out.println("上传文件");
        //设置文件上传的路径
       String realPath= req.getServletContext().getRealPath("/upload/");
        File file = new File(realPath);
        if(!file.exists()){
            file.mkdir();
        }
        String filename = upload.getOriginalFilename();

        try {
            upload.transferTo(new File(realPath,filename));
        } catch (IOException e) {
            e.printStackTrace();
        }
        //测试写在upload.jsp;
          return  "book/success";
    }
}
